package com.stylefeng.guns.modular.system.controller;

import com.stylefeng.guns.common.annotion.log.BussinessLog;
import com.stylefeng.guns.common.constant.Dict;
import com.stylefeng.guns.common.constant.tips.Tip;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.persistence.dao.JoinStepMapper;
import com.stylefeng.guns.common.persistence.dao.OpMessageMapper;
import com.stylefeng.guns.common.persistence.dao.OrgInfoMapper;
import com.stylefeng.guns.common.persistence.model.JoinStep;
import com.stylefeng.guns.common.persistence.model.OpMessage;
import com.stylefeng.guns.common.persistence.model.OrgInfo;
import com.stylefeng.guns.modular.system.dao.JoinStepDao;
import com.stylefeng.guns.modular.system.dao.OrgInfoDao;
import com.stylefeng.guns.modular.system.service.OrgInfoService;
import com.stylefeng.guns.modular.system.warpper.OrgInfoWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 入驻信息控制器
 *
 * @author fengshuonan
 * @Date 2018-08-13 14:46:27
 */
@Controller
@RequestMapping("/orgInfo")
public class OrgInfoController extends BaseController {

    private String PREFIX = "/system/orgInfo/";

    @Autowired
    OrgInfoMapper orgInfoMapper;
    @Autowired
    OrgInfoDao orgInfoDao;
    @Autowired
    OrgInfoService orgInfoService;
    @Autowired
    JoinStepMapper joinStepMapper;
    @Autowired
    JoinStepDao joinStepDao;
    @Autowired
    OpMessageMapper opMessageMapper;

    /**
     * 跳转到入驻信息首页
     */
    @RequestMapping("")
    public String index(Integer currentState, Model model) {
        model.addAttribute("currentStateId",currentState);
        return PREFIX + "orgInfo.html";
    }
    /**
     * 跳转到添加入驻信息
     */
    @RequestMapping("/orgInfo_add")
    public String orgInfoAdd() {
        return PREFIX + "orgInfo_add.html";
    }
    /**
     * 跳转到拒绝框
     */
    @RequestMapping("/orgInfo_refuse1")
    public String orgInfoRefuse(Model model,Integer joinId,Integer userId){
        model.addAttribute("joinId",joinId);
        model.addAttribute("userId",userId);
        return PREFIX + "refuse.html";}
    /**
     * 跳转到修改节点
     */

    @RequestMapping("/orgInfo_update/{orgInfoId}")
    public String OrgInfoUpdate(@PathVariable Integer orgInfoId,Integer currentStateId,Model model) {
        OrgInfo old=this.orgInfoMapper.selectById(orgInfoId);
        JoinStep joinStep = joinStepMapper.selectById(old.getJoinId());
        model.addAttribute("orgInfo",old);
        model.addAttribute("currentStateId",currentStateId);
        model.addAttribute("joinStep",joinStep);
        return PREFIX + "orgInfo_edit.html";
    }
    /**
     * 获取入驻信息列表
     * currentState 表示如下：
     * 0表示入驻中 即不包含 97,98,99
     * 97是待审核
     * 98是拒绝
     * 99是通过
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Integer currentState) {

            List<Map<String, Object>> orgInfos = orgInfoDao.selectByCurrentState(currentState);
            Object warp = new OrgInfoWarpper(orgInfos).warp();
            return warp;


    }
    /**
     * 新增入驻信息
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add() {
        return super.SUCCESS_TIP;
    }
    /**
    * 拒绝
    */
    @RequestMapping(value = "/refuse1")
    @ResponseBody
    public Object refuse1() {
        return super.SUCCESS_TIP;
    }
     /**
     * 删除入驻信息
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete() {
        return SUCCESS_TIP;
    }


    /**
     * 修改入驻信息
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "审批商户",key = "orgInfoId",dict = Dict.DictMap)
    public Object up(OrgInfo orgInfo,Integer currentState) {
        Integer update = orgInfoMapper.updateById(orgInfo);
        return super.SUCCESS_TIP;
    }

    /**
     * 入驻信息详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }

    /**
     * id 为机构id
     * 审核通过 pass
     */
    @RequestMapping(value = "/pass")
    @ResponseBody
    public Tip pass(Integer id,Integer userId) {
        OpMessage opMessage = new OpMessage();
        opMessage.setUserId(userId);
        opMessage.setMessage("你的审核已经通过，您的后台登陆账号：org"+id+"，密码：111111  请登陆后立即修改为自己定义的密码");
        opMessage.setIsRead("0");
        opMessage.setCreateTime(new Date());
        opMessageMapper.insert(opMessage);
//        return null;
        return orgInfoService.pass(id);
    }

    /**
     * 审核不通过 refuse
     */
    @RequestMapping(value = "/refuse",method = RequestMethod.POST)
    @ResponseBody
    public Tip refuse(Integer id ,String reason,Integer userId) {
        Integer is = userId;
        OpMessage opMessage = new OpMessage();
        opMessage.setUserId(userId);
        opMessage.setIsRead("0");
        opMessage.setMessage("您因"+reason+"被拒绝了");
        opMessage.setCreateTime(new Date());
        opMessageMapper.insert(opMessage);

        return orgInfoService.refuse(id,reason);
    }

}
